<template>
  <div>
    <el-tabs tabPosition="left" value="tab1" @tab-click="onTabClick">
      <el-tab-pane label="基础信息" name="tab1">
        <el-form
          :model="updateForm"
          :rules="rules"
          ref="updateForm"
          label-width="100px"
          class="demo-ruleForm"
          @submit.native.prevent
        >
          <el-row>
            <el-row>
              <el-col :xs="24" :sm="12" :md="12">
                <el-form-item label="服务名称" prop="flowName">
                  <el-input v-model="updateForm.apiName" placeholder="请输入服务名称" clearable></el-input>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="12">
                <el-form-item label="服务编码" prop="flowCode">
                  <el-input v-model="updateForm.apiCode" placeholder="请输入服务编码" clearable></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :xs="24" :sm="12" :md="12">
                <el-form-item label="类别" prop="name">
                  <select-tree placeholder="请选择服务类别" :selectedValue.sync="updateForm.apiType"
                               :treeList="treeList"
                               :props="treeProps"></select-tree>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :xs="24" :sm="24" :md="24">
                <el-form-item label="同步异步" prop="asyncMode">
                  <el-radio v-model="updateForm.asyncMode" label="1" value="1">同步</el-radio>
                  <el-radio v-model="updateForm.asyncMode" label="3" value="3">异步</el-radio>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :xs="24" :sm="12" :md="12">
                <el-form-item label="缓存周期" prop="sceneCode">
                  <el-input type="number" v-model="updateForm.cacheExpiresCycle" placeholder="请输入缓存周期(分钟)" min="0"
                            max="9999"></el-input>
                </el-form-item>
              </el-col>
              <el-col :xs="24" :sm="12" :md="12">
                <el-form-item label="缓存KEY" prop="sceneCode">
                  <el-input v-model="updateForm.cacheQueryItem" placeholder="缓存key"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :xs="24" :sm="12" :md="12">
                <el-form-item label="接口状态" prop="disabled">
                  <el-radio v-model="updateForm.disabled" :label="1" value="1">禁用</el-radio>
                  <el-radio v-model="updateForm.disabled" :label="0" value="0">启用</el-radio>
                </el-form-item>
              </el-col>
            </el-row>
          </el-row>
          <div style="height: 60px">

          </div>
        </el-form>

      </el-tab-pane>
      <el-tab-pane label="输入示例" name="tab2">
        <code-editor ref="tab2code" v-focus currentTheme="solarized-dark" :codeText.sync="updateForm.inputCaseText"
                     :toolbarShow="true"
                     height="55vh"></code-editor>
      </el-tab-pane>
      <el-tab-pane label="原始报文示例" name="tab4">
        <code-editor ref="tab4code" currentTheme="solarized-dark" :codeText.sync="updateForm.oldOutputCaseText"
                     :toolbarShow="true" height="55vh"></code-editor>
      </el-tab-pane>
      <el-tab-pane label="解码结果示例" name="tab3">
        <code-editor ref="tab3code" currentTheme="solarized-dark" :codeText.sync="updateForm.outputCaseText"
                     :toolbarShow="true" height="55vh"></code-editor>
      </el-tab-pane>
      <el-tab-pane label="编码输入" name="tab5">
        <code-editor ref="tab5code" currentTheme="solarized-dark" :codeText.sync="updateForm.inputTpl"
                     :toolbarShow="true" height="55vh"></code-editor>
      </el-tab-pane>
      <el-tab-pane label="解码输出" name="tab6">
        <code-editor ref="tab6code" currentTheme="solarized-dark" :codeText.sync="updateForm.outputTpl"
                     :toolbarShow="true" height="55vh"></code-editor>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>
<script>
  import HttpCode from '@/js/httpCode.js'
  import engineTbFlowPortApi from '@/apis/dpsApp/engineTbFlowPortApi.js'
  import CodeEditor from '@/components/codeEditor/CodeEditor'

  export default {
    name: 'EngineFlowPortUpdate',
    components: {
      SelectTree: () => import('@/components/selecttree/SelectTree.vue'),
      CodeEditor
    },
    data () {
      return {
        updateForm: {
          id: '',
          asyncMode: '3',
          disabled: 0,
          apiName: '',
          apiCode: '',
          outputMaterialList: [{
            id: '12987122',
            name: '王小虎',
            amount1: '234',
            amount2: '3.2',
            amount3: 10
          }, {
            id: '12987123',
            name: '王小虎',
            amount1: '165',
            amount2: '4.43',
            amount3: 12
          }, {
            id: '12987124',
            name: '王小虎',
            amount1: '324',
            amount2: '1.9',
            amount3: 9
          }, {
            id: '12987125',
            name: '王小虎',
            amount1: '621',
            amount2: '2.2',
            amount3: 17
          }, {
            id: '12987126',
            name: '王小虎',
            amount1: '539',
            amount2: '4.1',
            amount3: 15
          }]
        },
        treeProps: {
          children: 'children',
          label: 'typeName',
          valueKey: 'typeCode'
        },
        rules: {
          // flowName: [
          //   {required: true, message: '请输入产品名称', trigger: 'blur'},
          //   {min: 2, max: 32, message: '产品名称长度2-32字符', trigger: 'blur'}
          // ],
          // flowCode: [
          //   {required: true, message: '请输入用户昵称', trigger: 'change'}
          // ],
          // sceneCode: [
          //   {required: true, message: '请输入用户昵称', trigger: 'change'}
          // ]
        }
      }
    },
    created () {
      //可能需要特殊处理，手动赋值
      console.log(this.data)
      if (this.data != null) {
        this.updateForm = this.data
        this.updateForm.inputCaseText = JSON.stringify(this.data.inputCase)
        this.updateForm.oldOutputCaseText = JSON.stringify(this.data.oldOutputCase)
        this.updateForm.outputCaseText = JSON.stringify(this.data.oldOutputCase)
      }
    },
    methods: {
      submitForm () {
        this.$refs['updateForm'].validate(valid => {
          if (valid) {
            this.submitFormHandle()
          } else {
            this.cancelSubmitForm()
          }
        })
      },
      submitFormHandle () {
        try {
          this.updateForm.inputCase = JSON.parse(this.updateForm.inputCaseText)
          this.updateForm.oldOutputCase = JSON.parse(this.updateForm.oldOutputCaseText)
          this.updateForm.oldOutputCase = JSON.parse(this.updateForm.outputCaseText)
        } catch (e) {
          alert('json转换错误，请检查')
          this.cancelSubmitForm()
        }
        if (this.updateForm.id == undefined || this.updateForm.id == '') {
          engineTbFlowPortApi
            .addService(this.updateForm)
            .then(msg => {
              this.submitFormSuccess(msg)
            })
            .catch(msg => {
              if ((msg.cdoe = 400)) {
                this.formError(msg)
              }
              this.cancelSubmitForm()
            })
        } else {
          engineTbFlowPortApi.updateService(this.updateForm)
            .then(msg => {
              this.submitFormSuccess(msg)
            }).catch(msg => {
            if ((msg.cdoe = 400)) {
              this.formError(msg)
            }
            this.cancelSubmitForm()
          })
        }
      },
      submitFormSuccess (msg) {
        this.$store.commit('successSubmitForm', {
          name: this.formKey,
          data: msg
        })
      },
      cancelSubmitForm () {
        this.$store.commit('cancelSubmitForm', {name: this.formKey})
      },
      formError (msg) {
        if (msg.code == HttpCode.E400) {
          for (let row of msg.errors) {
            let fields = this.$refs['updateForm'].fields.filter(it => {
              return it.prop == row.errName
            })
            if (fields.length > 0) {
              fields[0].validateMessage = row.errMsg
              fields[0].validateState = 'error'
            }
          }
        }
      },
      onTabClick (tab) {
        this.$refs[tab.name + 'code'].refresh()  // 调用子组件的方法childClick
      }
    },
    props: {
      formKey: {
        type: String,
        default: 'currentForm'
      },
      data: {
        type: Object,
        default: {}
      },
      treeList: {
        type: Array,
        default: []
      },
    },
    computed: {
      submitFormEventListener () {
        return this.$store.getters.onEvent(`${this.formKey}-submitForm`)
      }
    },
    watch: {
      submitFormEventListener: function (a, b) {
        if (a == null || a == b) {
          return null
        }
        this.submitForm()
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 120px;
    height: 120px;
    line-height: 120px;
    text-align: center;
  }

  .avatar {
    width: 120px;
    height: 120px;
    display: block;
  }
</style>
